<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
model('jy_weishop/member')->checklogin();
$return['show_setmobile']=cfc('core')->check_setmobile(2);
$return['show_setmember']=cfc('core')->check_setmember(2);
if($_RQ['op']=='main'){
	$return['list']=array();
	$return['params']['status']=intval($_RQ['status']);
	$return['member']=model('core/member')->detail($_SESSION['uid'],array('mobile','avatar','nickname'));
	$address=pdo_get('core_address',array('isdel'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$address['pcd']=json_decode($address['pcd'],true);
	$return['address']=$address?:array();
	$stores = pdo_getall('jy_weishop_store',array('uniacid' =>$_SESSION['uniacid'],'status'=>1,'type'=>array(1,3)),'','','paixu desc,id desc');
	if($stores){
		foreach($stores as &$pv){
			$pv['pcd']=json_decode($pv['pcd'],true);
		}
		$return['stores']=$stores;
		$return['selectstore']=$stores['0'];
	}
	set_html('title','订单列表');
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if($_RQ['status']!=10){
		$params['status']=intval($_RQ['status']);
	}
	if($params['status']==1){
		$params['status']=array(1,2,3,4);
	}
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=intval($_RQ['ordersn']);
	}
	if(!empty($_RQ['userdeleted'])){
		$params['userdeleted']=1;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_kabao_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
	foreach($list as &$item){
		$item['jiangs']=json_decode($item['jiangs'],true);
		$item['sendtype']=2;
		$issendtype1=$issendtype2=$issendtype3=0;
		foreach($item['jiangs'] as &$sv){
			$sv['goods']['image']=tomedia($sv['goods']['image']);
			if($sv['type']<=0 && $sv['goods']['type']==0){
				$issendtype1=1;
			}
			if($sv['type']<=0 && ($sv['goods']['type']==1 || $sv['goods']['type']==2)){
				$issendtype3=1;
			}
			if($sv['type']<=0 && ($sv['goods']['type']==3)){
				$issendtype2=1;
			}
		}
		if($issendtype3){
			$item['sendtype']=3;
		}
		if($issendtype2){
			$item['sendtype']=2;
		}
		if($issendtype1){
			$item['sendtype']=1;
			$item['sendtypes']=array(
					'1'=>'快递配送',
					'2'=>'自提',
			);
			if($citypeisong['isopen']==1 && !$return['pluginset']['is_peisong']){
				$item['sendtypes'][1]='同城配送';
				$item['is_peisong']=1;
			}
		}
		//获取地址
		$item['address']=json_decode($item['address'],true);
		if($item['address'] && ($item['address']['address'] || $item['address']['mobile'])){	
			$address=$item['address'];
		}else{
			$address=$item['addressid'];
		}
		$item['addressinfo']=model('core/member')->get_address_str($address);
		$item['code']=pdo_get('jy_kabao_batch_code',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['codeid']));
		if($item['listid']){
			$item['kabao']=pdo_get('jy_kabao_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['listid']),array('id','image','title','price'));
		}else{
			$item['kabao']=json_decode($item['diyinfo'],true);
		}
		if($item['storeid']){
			$item['store']=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']),array('id','title'));
		}
		$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		$item['statustitle']=$this->orderstatus[$item['status']];
		$item['lognum']=pdo_count('jy_kabao_songlog',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id']));
		$item['lslognum']=$item['total']-$item['lognum'];
		$item['lsprice']=$item['lastnum']<$item['total']?$item['lastnum']*$item['payfee']/$item['total']:$item['payfee'];
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='tuikuan'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	model('jy_kabao/order')->refund_pay($id);
	exi('退款成功！','','/orderlist/');	
}
if($_RQ['op']=='getgoods'){
	$post=get_postdata();
	if($post){
		if($post['status']!=1){
			exi('该订单的状态，不支持当前操作！','error');	
		}
		if($post['sendtype']!=1 && empty($post['username'])){
			exi('请输入姓名','error');
		}
		if($post['sendtype']!=1 && empty($post['mobile'])){
			exi('请输入联系电话','error');
		}
		if($post['sendtype']!=1 && !preg_match("/^1[345789]\d{9}$/ims",$post['mobile'])){  	 
			exi('请输入正确的手机号码','error');
		}
		if($post['sendtype']==1 && (!$post['address']['pcd']['province'] || !$post['address'])){
			exi('请填收货地址！','error');
		}
		$update=array(		
				'username'=>$post['username']?:$post['address']['name'],
				'mobile'=>$post['mobile']?:$post['address']['mobile'],
				'address'=>ijson_encode($post['address']),
				'peisongfee'=>$post['sendtype']==1?$return['pluginset']['peisongfee']:0,
				'sendtype'=>$post['sendtype'],
				'storeid'=>$post['storeid'],
		);
		$jiangs=$post['jiangs'];
		$goodslist=array();
		foreach($jiangs as $item){
			if($item['type']<=0){
				$goodsfile=array('id','fang_credit2','no_de_sales','isdiyprice','diyprice','invoice','type','buylevels','buygroups','title','merchid','status','pcate','ccate','total','subtitle','image','video','price','totalcnf','weight','active_plugin','active_id','storeids','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit','isverify','diyfields','api_id','api_pluginname');
				$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid'],'uniacid'=>$_SESSION['uniacid']),$goodsfile);
				$goods['total']=1;
				$goods['goodsid']=$item['goodsid'];
				$goodslist[]=$goods;
			}
		}
		$new_order=array(
			'mid'=>$_SESSION['uid'],
			'remark'=>trim($post['remark']),
			'goods'=>$goodslist,
			'merchid'=>$post['merchid'],
			'sendtype'=>intval($post['sendtype'])?:1,
			'is_peisong'=>$post['is_peisong'],
			'mobile'=>$update['mobile'],
			'realname'=>$update['username'],
			'address'=>$update['address'],
			'storeid'=>$post['storeid'],
			'price'=>$update['peisongfee'],
			'dispatchprice'=>$update['peisongfee'],
			'plugin_name'=>'jy_kabao',
		);
		$order_id=model('jy_weishop/order')->create($new_order);
		$update['orderid']=$order_id;
		pdo_update('jy_kabao_order',$update,array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
		if($update['peisongfee']>0){
				$params['fee']=$update['peisongfee'];
				$params['title']='配送费';
				$params['id']=$post['id'];
				$params['plugin']='jy_kabao';
				$params['back_url']='jy_kabao/orderlist/payresult';
				$payid=cfc('pay')->create_pay_order($params);
				$this->pdo_update('order',array('pay_id'=>$payid),array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
				pdo_update('jy_weishop_order',array('pay_id'=>$payid),array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
				$res=array(
					'pay_id'=>$payid,
				);
				exi($res);
		}else{
			pdo_update('jy_kabao_order',array('status'=>$post['sendtype']==1?3:2),array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
			$kabao_order=pdo_get('jy_kabao_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['id']));
			$sn=pdo_getcolumn('jy_kabao_batch_code',array('uniacid'=>$_SESSION['uniacid'],'id'=>$kabao_order['codeid']),'sn');
			$params=array(
				'paytype'=>'codechange',
				'transid'=>$sn,
			);
			model('jy_weishop/order')->order_pay($order_id,$params);
			exi('提交成功！','','/orderlist/');
		}	
	}else{
	   exi('请完善购买信息','error');
	}
}
if($_RQ['op']=='pay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_kabao_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi($order['pay_id']);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_kabao_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['peisongfee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	$sn=pdo_getcolumn('jy_kabao_batch_code',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['codeid']),'sn');
	$params=array(
		'paytype'=>'codechange',
		'transid'=>$sn,
	);
	//$this->pdo_update('order',array('status'=>$order['sendtype']==1?3:2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id']));
	model('jy_weishop/order')->order_pay($order['orderid'],$params);
	exi('支付成功');
}
?>